/*
 * Copyright 2020-2021 redragon-erp/赤龙ERP
 * Author redragon/dongbin
 * This file is part of redragon-erp/赤龙ERP.

 * 赤龙ERP官方声明：
 * (1)赤龙ERP的知识产权归软件著作权人所有
 * (2)未经著作权人授权，不得将赤龙ERP用于任何形式的商业用途
 * (3)对侵权的组织或个人，著作权人将保留诉诸法律的权利

 * 赤龙ERP官网详见 <https://www.redragon-erp.com>
 * 赤龙ERP仓库详见 <https://gitee.com/redragon/redragon-erp> 
 * 如遇任何问题，请与著作权人联系
 */
package com.erp.oa.notice.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.Valid;

import org.redragon.framework.util.ShiroUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.redragon.framework.springmvc.ControllerSupport;
import org.apache.commons.lang.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.redragon.framework.hibernate.data.RDDataBox;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;
import com.erp.dataset.service.DatasetCommonService;
import com.erp.hr.dao.model.HrDepartment;
import com.erp.hr.dao.model.HrStaff;
import com.erp.hr.dao.model.HrStaffDepartmentR;
import com.erp.hr.service.HrCommonService;
import com.erp.hr.service.HrDepartmentService;
import com.erp.hr.service.HrStaffDepartmentRService;
import com.erp.hr.service.HrStaffService;
import com.erp.oa.notice.dao.data.DataBox;
import com.erp.oa.notice.dao.model.SysNotice;
import com.erp.oa.notice.dao.model.SysNoticeCO;
import com.erp.oa.notice.dao.model.SysNoticeRecord;
import com.erp.oa.notice.service.SysNoticeRecordService;
import com.erp.oa.notice.service.SysNoticeService;

@Controller
@RequestMapping("/web/sysNotice")
public class SysNoticeWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(SysNoticeWebController.class);
    
    //服务层注入
    @Autowired
    private SysNoticeService sysNoticeService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private HrCommonService hrCommonService;
    @Autowired
    private HrDepartmentService hrDepartmentService;
    @Autowired
    private HrStaffService hrStaffService;
    @Autowired
    private HrStaffDepartmentRService hrStaffDepartmentRService;
    @Autowired
    private SysNoticeRecordService sysNoticeRecordService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getSysNoticeList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-04-05 19:46:56
     * @author 
     * @param pages
     * @param sysNoticeCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getSysNoticeList")
    public String getSysNoticeList(Pages pages, SysNoticeCO sysNoticeCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        sysNoticeCO.setCreatedBy(ShiroUtil.getPrincipal());
        //分页查询数据
        List<SysNotice> sysNoticeList = this.sysNoticeService.getDataObjects(pages, sysNoticeCO);
        
        //通知类型
        Map noticeTypeMap = DataBox.getNoticeType();
        //业务类型
        Map businessTypeMap = DataBox.getBusinessType();
        //通知等级
        Map noticeLevelMap = DataBox.getNoticeLevel();
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        
        //页面属性设置
        model.addAttribute("sysNoticeList", sysNoticeList);
        model.addAttribute("pages", pages);
        model.addAttribute("noticeTypeMap", noticeTypeMap);
        model.addAttribute("businessTypeMap", businessTypeMap);
        model.addAttribute("noticeLevelMap", noticeLevelMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        
        return "basic.jsp?content=sysNotice/sysNoticeList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-04-05 19:46:56
     * @author 
     * @param sysNotice
     * @param model
     * @return String
     *
     */
    @RequestMapping("getSysNotice")
    public String getSysNotice(SysNotice sysNotice, Model model) {
        //查询要编辑的数据
        if(sysNotice!=null&&sysNotice.getNoticeId()!=null) {
            sysNotice = this.sysNoticeService.getDataObject(sysNotice.getNoticeId());
        }else {
            sysNotice.setNoticeCode(SnowFlake.getConcurrentId().toString());
            sysNotice.setPublishFlag("N");
        }
        
        //通知类型
        Map noticeTypeMap = DataBox.getNoticeType();
        //业务类型
        Map businessTypeMap = DataBox.getBusinessType();
        //通知等级
        Map noticeLevelMap = DataBox.getNoticeLevel();
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        //获取部门
        List<HrDepartment> hrDepartmentList = this.hrDepartmentService.getDataObjects();
        //获取职员
        List<HrStaff> hrStaffList = this.hrStaffService.getDataObjects();
        
        //页面属性设置
        model.addAttribute("sysNotice", sysNotice);
        model.addAttribute("noticeTypeMap", noticeTypeMap);
        model.addAttribute("businessTypeMap", businessTypeMap);
        model.addAttribute("noticeLevelMap", noticeLevelMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        model.addAttribute("hrDepartmentList", hrDepartmentList);
        model.addAttribute("hrStaffList", hrStaffList);
        
        return "basic.jsp?content=sysNotice/sysNoticeEdit";
    }
    @RequestMapping("getSysNoticeMain")
    public String getSysNoticeMain(SysNotice sysNotice, Model model) {
        sysNotice = this.sysNoticeService.getDataObject(sysNotice.getNoticeCode());
        //通知类型
        Map noticeTypeMap = DataBox.getNoticeType();
        //业务类型
        Map businessTypeMap = DataBox.getBusinessType();
        //通知等级
        Map noticeLevelMap = DataBox.getNoticeLevel();
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        //获取部门
        List<HrDepartment> hrDepartmentList = this.hrDepartmentService.getDataObjects();
        //获取职员
        List<HrStaff> hrStaffList = this.hrStaffService.getDataObjects();

        //页面属性设置
        model.addAttribute("sysNotice", sysNotice);
        model.addAttribute("noticeTypeMap", noticeTypeMap);
        model.addAttribute("businessTypeMap", businessTypeMap);
        model.addAttribute("noticeLevelMap", noticeLevelMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        model.addAttribute("hrDepartmentList", hrDepartmentList);
        model.addAttribute("hrStaffList", hrStaffList);

        return "basic.jsp?content=sysNotice/sysNoticeView";
    }
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-04-05 19:46:56
     * @author 
     * @param sysNotice
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editSysNotice")
    public String editSysNotice(@Valid SysNotice sysNotice, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getSysNotice";
        }
        
        try {
            //对当前编辑的对象初始化默认的字段
            
            //保存编辑的数据
            sysNotice.setCreatedBy(ShiroUtil.getPrincipal());
            this.sysNoticeService.insertOrUpdateDataObject(sysNotice);
            //修改为提交状态
            //updateApproveStatus(sysNotice.getNoticeId(),"SUBMIT",attr);
            //修改为通过审核状态
            updateApproveStatus(sysNotice.getNoticeId(),"APPROVE",attr);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("noticeId", sysNotice.getNoticeId());
        }catch(Exception e){
            if(e.getCause().getClass()==ConstraintViolationException.class) {
                //提示信息
                model.addAttribute("hint", "hint");
                model.addAttribute("alertMessage", "编码已存在，请重新输入");
                return "forward:getSysNotice";
            }else {
                throw e;
            }
        }
        
        return "redirect:getSysNotice";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-04-05 19:46:56
     * @author 
     * @param sysNotice
     * @return String
     *
     */
    @RequestMapping("deleteSysNotice")
    public String deleteSysNotice(SysNotice sysNotice, RedirectAttributes attr) {
        //删除数据前验证数据
        if(sysNotice!=null&&sysNotice.getNoticeId()!=null) {
            //删除数据
            this.sysNoticeService.deleteDataObject(sysNotice);
            //提示信息
            attr.addFlashAttribute("hint", "success");
        }
        
        return "redirect:getSysNoticeList";
    }
    
    
    
    /**
     * 
     * @description 更新审批状态
     * @date 2020年8月4日
     * @author dongbin
     * @param id
     * @param approveStatus
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("updateApproveStatus")
    public String updateApproveStatus(Integer id, String approveStatus, RedirectAttributes attr) {
        
        if(id!=null&&StringUtils.isNotBlank(approveStatus)) {
            //更新审核状态
            this.sysNoticeService.updateApproveStatus(id, approveStatus);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("noticeId", id);
        }else {
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", "提交或审批数据错误");
            attr.addAttribute("noticeId", id);
        }
        
        return "redirect:getSysNotice";
    }
    
    
    
    /**
     * 
     * @description 发布通知
     * @date 2021年4月6日
     * @author dongbin
     * @param id
     * @param attr
     * @return String
     *
     */
    @RequestMapping("publishNotice")
    public String publishNotice(Integer id, RedirectAttributes attr) {
        
        if(id!=null) {
            //默认通过审批不用在审批
            this.sysNoticeService.updateApproveStatus(id, "APPROVE");
            String result = this.sysNoticeRecordService.publishNoticeRecord(id);
            if(StringUtils.isNotBlank(result)) {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", result);
                attr.addAttribute("noticeId", id);
            }else {
                //提示信息
                attr.addFlashAttribute("hint", "success");
                attr.addAttribute("noticeId", id);
            }
        }
        
        return "redirect:getSysNotice";
    }
}
